package com.xs.ahoi.hcxs.model.vo;

import com.xs.ahoi.hcxs.model.enums.FileTypeEnum;
import com.xs.ahoi.hcxs.utils.FileUtil;
import com.xs.ahoi.hcxs.utils.RegexConstants;

import java.io.File;

/**
 * @author fuhuichao
 * @date 2020-01-17 10:23
 */
public class ImportFileItemVO {

    private boolean imported;
    private boolean zip;
    private String name;
    private String path;
    private String size;
    private FileTypeEnum fileType;

    public static ImportFileItemVO convertFromFile(File file) {
        ImportFileItemVO vo = new ImportFileItemVO();
        vo.setImported(false);
        vo.setName(file.getName());
        vo.setPath(file.getAbsolutePath());
        vo.setZip(RegexConstants.zipPattern.matcher(file.getName()).find());
        if (file.isFile()) {
            vo.setSize(FileUtil.byteToSize(file.length()));
            vo.setFileType(FileTypeEnum.FILE);
        } else {
            vo.setSize(FileUtil.getChildFiles(file.getAbsolutePath()).size() + "项");
            vo.setFileType(FileTypeEnum.FOLDER);
        }
        return vo;
    }

    public boolean isImported() {
        return imported;
    }

    public void setImported(boolean imported) {
        this.imported = imported;
    }

    public boolean isZip() {
        return zip;
    }

    public void setZip(boolean zip) {
        this.zip = zip;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public FileTypeEnum getFileType() {
        return fileType;
    }

    public void setFileType(FileTypeEnum fileType) {
        this.fileType = fileType;
    }

    /**
     * 是否不能被导入
     */
    public boolean canNotImport() {
        return this.isImported() || this.isZip();
    }
}
